
(ns gui.gui-ns
  (:use logika.logika-ns)
  )

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; Nesto o problemu 8 kraljica, problem osam kraljica je problem koji je stariji i od same sahovske igre
;;; ideja problema je da se na odredjenoj tabli AxA postavi A elemenata a da se oni ne sudaraju po koloni redu kao i njihovim dijagonalama
;;; u mom studiskom primjeru algoritam koji je preuzet sa interneta resava mogucnost nXn postavljanja kraljica
;;; medjutim on je ogranicen na resavanje 8X8 table,
;;; ovaj problem od 8x8 je moguce resiti na 92 nacina,
;;; u programu imamo mogucnost resavanja problema od 1 do 92 pomocu dugmeta "Rijesi sledeci"
;;; Detaljni kod ce biti opbjasnjen kroz niz komentara

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; Graficko rijesenje sahovske table, menija

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;



(import '(javax.swing JFrame JTextField JOptionPane JMenuItem)
  '(java.awt.event ActionListener)
  '(java.awt ComponentOrientation)
  '(javax.swing.event MenuListener)
  '(java.awt GridLayout Component FileDialog Color Font))





(def counter-begin
  (let [count (ref 0)] #(dosync (alter count inc)))

  )

(def counter-end
  (let [count (ref 92)] #(dosync (alter count dec)))

  )




(defn swing-components [container]
  (for [i (range (.getComponentCount container))]
       (.getComponent container i)))

(defn clear-frame [frame]
    "Function witch clear all components of frame"
       (doseq [ panel  (swing-components (.getContentPane frame))]
              (doseq [component (swing-components panel)]
                 (doto component
                    (.setText (str ""))
                    (.setCaretPosition 0))
                )
         )
  )

; prosta definicija teksta koji opisuje aplikaciju
(def *about*
    "Example of 8 queen solution in Colure+Swing")



(defmacro action [component action]
    "Add action listener "
    `(.addActionListener ~component
      (proxy [ActionListener] []
       (~'actionPerformed [~'evt] ~action))))



(defn info-message [text]
  (JOptionPane/showMessageDialog nil, text))



  (defn create-field
    []
  (let [table (javax.swing.JPanel. (java.awt.GridLayout. 8 8))]

       (doseq [counter-column (range 8)] 
         (doseq [counter-row (range 8)]
         (let [field (JTextField.)] 
         (.setHorizontalAlignment field JTextField/CENTER) 
         (.setEditable field false) ;
         (if (zero? (mod counter-column 2) ) 
         (if (zero? (mod counter-row 2) )
         (.setBackground field (java.awt.Color/yellow)))) 
         (if (= 1 (mod counter-column 2) ) ;
         (if (= 1 (mod counter-row 2) )
         (.setBackground field (java.awt.Color/yellow))))
           
         (.setFont field (java.awt.Font. "SansSerif", java.awt.Font/BOLD, 15))
           
         (.add table field )) ) )
       table))



(defn create-frame []
  (let [frame (javax.swing.JFrame. "Queen Problem")]
       (.add frame (create-field)) 
       (doto frame
       (.setLayout (java.awt.GridLayout. 1 1 1 1)) 
       (.setForeground (java.awt.Color/black)) 
       (.setSize 400 400) 
      (.setDefaultCloseOperation JFrame/EXIT_ON_CLOSE) 
       )

       frame))





(defn create-table-gui ([]

      "Generate Chess board"

      (let [frame (create-frame) 
           bar (javax.swing.JMenuBar.) 
           solve-problem (JMenuItem. "Solve problem") 
           solve-next (JMenuItem. "Solve next")
           solve-last (JMenuItem. "Solve last solution") 
           solve-previus (JMenuItem. "Solve previsu") 
           clear (JMenuItem. "Clear board") 
           about (JMenuItem. "About application") 
           exit (JMenuItem. "Quit") 
           meni (javax.swing.JMenu. "Options")] 

        


       
        (action exit (.dispose frame)) 

        (action about (info-message *about*)) 

        (action solve-problem (
                                 (clear-frame frame) 
                                 (print 0 frame)
                                 )
          )

        (action solve-last (
                                 (clear-frame frame) 
                                 (print 91 frame)
                                 )
          )

        (action clear (clear-frame frame)) 

        (action  solve-next (
                                 (clear-frame  frame) 
                                 (print (counter-begin) frame)
                                 )
          )
         (action solve-previus (
                                 (clear-frame  frame) 
                                 (print (counter-end) frame)
                                 )
          )
           (doto meni 
           (.add solve-problem)
           (.add solve-next )
           (.add solve-last)
           (.add solve-previus)
           (.add clear)
           (.add about)
           (.add exit))
           (doto bar
           (.add meni))
           (doto frame
           (.setJMenuBar bar)
           (.applyComponentOrientation ComponentOrientation/LEFT_TO_RIGHT)
           (.setVisible true))
           frame)))




(defn -main-fn [] (create-table-gui))


